package com.gitee.srurl.core.factory.id;

import cn.hutool.core.util.StrUtil;
import com.gitee.srurl.core.api.IConfigCache;
import com.gitee.srurl.core.api.IUniqueIdGenerate;
import com.gitee.srurl.core.api.impl.id.OidGenerate;
import com.gitee.srurl.core.api.impl.id.SidGenerate;
import com.gitee.srurl.core.api.impl.id.SnowflakeIdGenerate;
import com.gitee.srurl.core.properties.OidProperties;
import com.gitee.srurl.core.properties.SidProperties;
import com.gitee.srurl.core.properties.SnowflakeProperties;
import com.gitee.srurl.core.util.SpringContextUtil;
import lombok.AllArgsConstructor;
import lombok.Getter;

import java.util.List;

/**
 * id生成算法的枚举类
 *
 * @author zxw
 * @date 2023/5/8 21:44
 */
@Getter
@AllArgsConstructor
public enum IIdStrategyEnums {

    /**
     * 雪花算法
     */
    SNOWFLAKE("snowflake") {
        @Override
        public IUniqueIdGenerate getUniqueIdGenerate(List<IConfigCache> configCacheList) {
            SnowflakeProperties snowflakeProperties = SpringContextUtil.getBean(SnowflakeProperties.class);
            return new SnowflakeIdGenerate(configCacheList, snowflakeProperties);
        }
    },

    /**
     * short id
     */
    SID("sid") {
        @Override
        public IUniqueIdGenerate getUniqueIdGenerate(List<IConfigCache> configCacheList) {
            SidProperties sidProperties = SpringContextUtil.getBean(SidProperties.class);
            return new SidGenerate(configCacheList, sidProperties);
        }
    },

    /**
     * order id
     */
    OID("oid") {
        @Override
        public IUniqueIdGenerate getUniqueIdGenerate(List<IConfigCache> configCacheList) {
            OidProperties oidProperties = SpringContextUtil.getBean(OidProperties.class);
            return new OidGenerate(configCacheList, oidProperties);
        }
    },

    ;

    /**
     * 类型
     */
    private final String type;

    public abstract IUniqueIdGenerate getUniqueIdGenerate(List<IConfigCache> configCacheList);

    public static IIdStrategyEnums getInstanceByType(String type) {

        if (StrUtil.isBlank(type)) {
            return null;
        }

        IIdStrategyEnums[] values = values();
        for (IIdStrategyEnums idStrategyContextEnums : values) {
            if (StrUtil.equals(idStrategyContextEnums.getType(), type)) {
                return idStrategyContextEnums;
            }
        }

        return null;
    }
}
